/**
 * 19.12.9编写
 */
let THREE=require('three');
import EffectComposer from "../postprocessing/effect-composer";
import RenderPass from "../postprocessing/render-pass";
import OutlinePass from '../postprocessing/outline-pass';
import ShaderPass from "../postprocessing/shader-pass";
import FXAAShader from "../shaders/FXAA-shader";

/**
 * 轮廓高亮显示
 * @param renderer {renderer} 渲染器
 * @param scene {scene} 渲染的场景
 * @param camera {camera} 渲染的相机
 * @param size {object:{width,height}} 尺寸
 */
export default class Outline{
    constructor(renderer,scene,camera,size){
        this._renderer=renderer;
        this._scene=scene;
        this._camera=camera;
        this._size=size;
        this._init();
        console.warn('this class is deprecated now ,use OutlineSelective instead!');
    }

    /**
     * 设置闪烁间隔（秒）
     * @param num
     */
    set pulsePeriod(num){
        this._outlinePass.pulsePeriod=num;
    }

    /**
     * 可见轮廓颜色
     * @param color
     */
    set visibleEdgeColor(color){
        this._outlinePass.visibleEdgeColor.copy(color);
    }

    /**
     * 遮挡的轮廓颜色
     * @param color
     */
    set hiddenEdgeColor(color){
        this._outlinePass.hiddenEdgeColor.copy(color);
    }

    /**
     * 光晕大小
     * @param num
     */
    set edgeGlow(num){
        this._outlinePass.edgeGlow=num;
    }

    /**
     * 使用贴图样式
     * @param bol
     */
    set usePatternTexture(bol){
        this._outlinePass.usePatternTexture=bol;
    }

    /**
     * 贴图样式
     * @param texture
     */
    set patternTexture(texture){
        this._outlinePass.patternTexture=texture;
    }

    /**
     * 轮廓厚度
     * @param num
     */
    set edgeThickness(num){
        this._outlinePass.edgeThickness=num;
    }

    /**
     * 轮廓强度
     * @param num
     */
    set edgeStrength(num){
        this._outlinePass.edgeStrength=num;
    }

    /**
     * 采样率（像素次数）
     * @param num
     */
    set downSampleRatio(num){
        this._outlinePass.downSampleRatio=num;
    }

    /**
     * 分辨率
     * @param vec2
     */
    set resolution(vec2){
        this._outlinePass.resolution.copy(vec2);
    }

    /**
     * 窗体尺寸改变
     * @param size {object:{width,height}}
     */
    set size(size){
        if(size.width===this._size.width&&size.height===this._size.height)return;
        this._size.width=size.width;
        this._size.height=size.height;
        this._antialiasPass.uniforms['resolution'].value.set(1/this._size.width,1/this._size.height);
        this._composer.setSize(this._size.width,this._size.height);
    }

    /**
     * 获取composer用于渲染
     * @returns {null|EffectComposer}
     */
    get composer(){
        return this._composer;
    }

    /**
     * 添加显示轮廓的物体
     * @param meshes
     */
    addTarget(...meshes){
        for(let m of meshes){
            let id=this._targets.findIndex((t)=>t.uuid===m.uuid);
            if(id>-1)continue;
            this._targets.push(m);
        }
    }

    /**
     * 移除显示轮廓的物体
     * @param meshes
     */
    removeTarget(...meshes){
        for(let m of meshes){
            let id=this._targets.findIndex((t)=>t.uuid===m.uuid);
            if(id<0)continue;
            this._targets.splice(id,1);
        }
    }

    /**
     * 清除显示轮廓的物体
     */
    clearTarget(){
        this._targets=[];
    }

    /**
     * 销毁
     */
    dispose(){
        this._outlinePass.dispose();
        this._targets=null;
        this._composer=null;
        this._mainPass=null;
        this._outlinePass=null;
        this._antialiasPass=null;
    }

    _init(){
        this._initVariable();
        this._initComponents();
    }
    _initVariable(){
        this._targets=[];
        this._composer=null;
        this._mainPass=null;
        this._outlinePass=null;
        this._antialiasPass=null;
    }
    _initComponents(){
        this._initComposer();
        this._initMainPass();
        this._initOutlinePass();
        this._initAntialiasPass();
    }
    _initComposer(){
        this._composer=new EffectComposer(this._renderer);
    }
    _initMainPass(){
        this._mainPass=new RenderPass(this._scene,this._camera);
        this._composer.addPass(this._mainPass);
    }
    _initOutlinePass(){
        this._outlinePass=new OutlinePass(this._scene,this._camera,new THREE.Vector2(256,256),this._targets);
        this._composer.addPass(this._outlinePass);
    }
    _initAntialiasPass(){
        this._antialiasPass=new ShaderPass(FXAAShader);
        this._antialiasPass.uniforms['resolution'].value.set(1/this._size.width,1/this._size.height);
        this._composer.addPass(this._antialiasPass);
    }
}